package com.example.server;

import java.sql.Date;
import java.text.DateFormat;
import java.util.ArrayList;

import com.example.silvercare.R;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import app.database.friendlist.Constants;
import app.database.friendlist.FriendListDB;

public class Notification1ListActivity extends ListActivity {
	final int TABLE = 0;
	FriendListDB dba;
	FriendListAdapter adapter;
	
	private class Friend{
		public String name;
		public String email;
		public String address;
		public String time;
		public Friend(String n, String e, String a, String t){
			name = n;
			email = e;
			address = a;
			time = t;
		}
		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		// TODO Auto-generated method stub
		dba = new FriendListDB(this);
		dba.open();
		setContentView(R.xml.friendlist);

		super.onCreate(savedInstanceState);
		adapter=new FriendListAdapter(this);
		this.setListAdapter(adapter);
		
		registerForContextMenu(getListView());
	}
	
	private class FriendListAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		public ArrayList<Friend> friendlist;
		public FriendListAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			friendlist = new ArrayList<Friend>();
			getdata();
		}
		public void getdata(){
			Cursor c = dba.getFriends(TABLE);
			startManagingCursor(c);
			if(c.moveToFirst()){
				do{
					String name = c.getString(c.getColumnIndex(Constants.FRIEND_NAME));
					String email = c.getString(c.getColumnIndex(Constants.EMAIL_NAME));
					String address = c.getString(c.getColumnIndex(Constants.NOTIFICATION_NAME));
					String time = c.getString(c.getColumnIndex(Constants.DATE_NAME));
					Friend f = new Friend(name, email, address, time);
					friendlist.add(f);
				}while(c.moveToNext());
			}
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return friendlist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return friendlist.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			final ViewHolder holder;
			View v  = convertView;
			if((v==null) || (v.getTag()==null)){
				v = mInflater.inflate(R.xml.elnotificationrow, null);
				holder = new ViewHolder();
				holder.mName = (TextView)v.findViewById(R.id.elnotificationname);
				holder.mEmail = (TextView)v.findViewById(R.id.elnotificationemail);
				holder.mAddress = (TextView)v.findViewById(R.id.elnotificationaddress);
				v.setTag(holder);
			}else{
				holder = (ViewHolder)v.getTag();
			}
			holder.mfriend = (Friend) getItem(position);
			holder.mName.setText(holder.mfriend.name);
			holder.mEmail.setText(holder.mfriend.email);
			holder.mAddress.setText("Address:"+holder.mfriend.address);
			v.setTag(holder);
			return v;
		}
		
		public class ViewHolder {
			Friend mfriend;
			TextView mName;
			TextView mEmail;
			TextView mAddress;
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		dba.close();
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		adapter=new FriendListAdapter(this);
		this.setListAdapter(adapter);
		super.onRestart();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, R.id.menu_editnotification1, 0, "Edit Notification");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onContextItemSelected(item);
		
		switch(item.getItemId()){
		case R.id.menu_editnotification1:
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		    int index = info.position;
		    Friend f = adapter.friendlist.get(index);
			Intent i = new Intent("com.example.server.Notification1");
			i.putExtra("NAME", f.name);
			i.putExtra("EMAIL", f.email);
			i.putExtra("ADDRESS", f.address);
			i.putExtra("TIME", f.time);
			i.putExtra("TABLE", TABLE);
			startActivity(i);
			break;
		}
		return true;
	}

}